﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class SAPDfctPrdDAL
    {
        public static IDataReader GetList(int DfctID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPDfctPrd_GetList", DfctID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPDfctPrd_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static int AddFctry(SAPDfctPrdFctryInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPDfctPrdFctry_Add");
            DB.AddInParameter(dbCommand, "DfctID", DbType.Int32, obj.DfctID);
            DB.AddInParameter(dbCommand, "Type", DbType.Int16, obj.Type);
            DB.AddInParameter(dbCommand, "Quantity", DbType.Decimal, obj.Quantity);
            DB.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, obj.CreateDate);
            DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool DeleteFctry(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPDfctPrdFctry_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetFctryAll(int DfctID, short type)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPDfctPrdFctry_GetAll", DfctID, type);
            return DB.ExecuteReader(dbCommand);
        }
    }
}
